import { Dispatch, Reducer, useState } from "react";
import { Action, State } from "../Messenger/interface";

function useReducer(reducer: Reducer<State, Action>, initialState: State): [State, Dispatch<Action>] {
  const [state, setState] = useState(initialState);

  const dispatch = (action: Action) => {
    setState(s => reducer(s, action));
  };

  return [state, dispatch];
}

export default useReducer;
